/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.acme.controller;

import com.acme.business.StockBusiness;
import com.acme.business.StockPriceBusiness;
import com.acme.model.Stock;
import com.acme.model.StockPrice;
import com.acme.persistence.StockPriceDao;
import com.acme.rest.QuoteVO;
import com.acme.rest.StockVO;
import com.acme.rest.YQLclient;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Transactional
@Controller  
public class StockPriceController {
	
    @Autowired
    private StockPriceDao stockPriceDao;
    @Autowired
    private StockPriceBusiness stockPriceBusiness;
    @Autowired
    private StockBusiness stockBusiness;

    @RequestMapping(value = "/StockPrice/{symbol}", method = RequestMethod.GET)
    public String listStockPrice(@PathVariable("symbol") String symbol, ModelMap model) throws Exception {      
        
        //ApplicationContext context = new ClassPathXmlApplicationContext("/mvc-dispatcher-servlet.xml");
        //StockBusiness stockBusiness = (StockBusiness) context.getBean("stockBusiness"); 
        //StockPriceBusiness stockPricesBusiness = (StockPriceBusiness) context.getBean("StockPricesBusiness"); 
        
        //stockBusiness.createStock("TS.BA", "Tenaris", "Metalurgica");
        //stockBusiness.findBySymbol(null);
        //List<Stock> argentinianStocks = stockBusiness.getAllStocks();
        
        YQLclient client = new YQLclient();
        StockVO result;
        
        Stock stock;
        //for (Iterator<Stock> it = argentinianStocks.iterator(); it.hasNext();) {
            //stock = it.next();
            //stock = stockBusiness.getStock(1);
            List<Stock> lstStock = stockBusiness.findBySymbol("TS.BA");
            stock = lstStock.get(0);
            result = (StockVO)client.doCall("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22" + stock.getSymbol() + "%22)%20and%20startDate%3D%222011-2-12%22%20and%20endDate%3D%222011-2-15%22%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env", StockVO.class);
            
            QuoteVO quote;
            StockPrice stockPrice;
            for (Iterator<QuoteVO> it1 = result.getQuotes().iterator(); it1.hasNext();) {
                quote = it1.next();
                stockPrice = new StockPrice();
                stockPrice.setStock_id(stock.getStock_id());
                stockPrice.setAdjustedClosePrice(Double.toString(quote.getAdjClose()));
                stockPrice.setClosePrice(Double.toString(quote.getClose()));
                stockPrice.setHighPrice(Double.toString(quote.getHigh()));
                stockPrice.setLowPrice(Double.toString(quote.getLow()));
                stockPrice.setMarket_id(02);
                stockPrice.setOpenPrice(Double.toString(quote.getOpen()));
                stockPrice.setPriceDate(quote.getDate().toString());
                stockPrice.setVolume(Long.toString(quote.getVolume()));
                
                //stockPriceBusiness.createStockPrice(stockPrice);
                System.out.println("size: " + stockPriceBusiness.getAllStockPrice().size());
                model.addAttribute("stock", stock);
                model.addAttribute("stockPriceDao", stockPriceBusiness.getAllStockPrice());
            }                   
        //}
        model.addAttribute("stockPriceDao", stockPriceBusiness.getAllStockPrice());
        return "stockPrice";
    }

    @RequestMapping(value = "/addStockPrice", method = RequestMethod.POST)
    public String addStock(@ModelAttribute("stockPrice") StockPrice stockPrice, BindingResult result) {

    	stockPriceDao.create(stockPrice);

        return "redirect:/";
    }

    @RequestMapping(value = "/updateStockPriceFromYahoo", method = RequestMethod.GET)
    public String updateStockPriceFromYahoo(@ModelAttribute("stock") Stock stock, BindingResult result)  throws Exception {        
        
        //ApplicationContext context = new ClassPathXmlApplicationContext("/mvc-dispatcher-servlet.xml");
        //StockPriceBusiness stockPricesBusiness = (StockPriceBusiness) context.getBean("StockPricesBusiness"); 
        
        YQLclient client = new YQLclient();
        StockVO stockVO;

        stockVO = (StockVO)client.doCall("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22SAMI.BA%22)%20and%20startDate%3D%222011-2-12%22%20and%20endDate%3D%222011-2-15%22%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env", StockVO.class);

        QuoteVO quote;
        StockPrice stockPrice;
        for (Iterator<QuoteVO> it1 = stockVO.getQuotes().iterator(); it1.hasNext();) {
            quote = it1.next();
            stockPrice = new StockPrice();
            stockPrice.setAdjustedClosePrice(Double.toString(quote.getAdjClose()));
            stockPrice.setClosePrice(Double.toString(quote.getClose()));
            stockPrice.setHighPrice(Double.toString(quote.getHigh()));
            stockPrice.setLowPrice(Double.toString(quote.getLow()));
            stockPrice.setMarket_id(02);
            stockPrice.setOpenPrice(Double.toString(quote.getOpen()));
            stockPrice.setPriceDate(quote.getDate().toString());
            stockPrice.setVolume(Long.toString(quote.getVolume()));

            stockPriceBusiness.createStockPrice(stockPrice);

        }                   
     
        return "redirect:/";
    }
    
    //@RequestMapping("/delete/{stockId}")
    public String deleteAlumno(@PathVariable("stockId") Long stockId) {

    	stockPriceDao.delete(stockPriceDao.findById(stockId));

        return "redirect:/";
    }

}
